package com.canbot.u05.activity.presidemode;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.canbot.u05.R;
import com.canbot.u05.utils.ToastTools;


/**
 * @author zmp
 * 主持人模式动作选择模块
 */
public class PresideChooseAction extends BaseCompereActivity implements View.OnClickListener{

        private static final String TAG = "PresideChooseAction";

        private RelativeLayout rlExistAudio, rlTextToSpeech;

        private CheckBox checkBox1, checkBox2;

        private ImageView btnBack;

        private Button btnFinish;

        private String actionID;

        private String actionName;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_choose_action);
                setViews();
                Intent intent = getIntent();
                actionID = intent.getStringExtra("actionID");
                actionName = intent.getStringExtra("actionName");
                initCheck();
        }

        private void initCheck() {
                if((!TextUtils.isEmpty(actionID)) && (!"-1".equals(actionID))) {
                        checkBox2.setChecked(true);
                        checkBox1.setChecked(false);
                } else {
                        checkBox2.setChecked(false);
                        checkBox1.setChecked(true);
                }
        }

        private void setViews() {
                rlExistAudio = (RelativeLayout) findViewById(R.id.host_mode_rl_audio);
                rlTextToSpeech = (RelativeLayout) findViewById(R.id.host_mode_rl_text_to_speech);
                checkBox1 = (CheckBox) findViewById(R.id.cbox_host_mode_choose_exist_audio);
                checkBox2 = (CheckBox) findViewById(R.id.cbox_host_mode_text_to_speech);
                btnBack = (ImageView) findViewById(R.id.host_mode_custom_imageView_back);
                btnFinish = (Button) findViewById(R.id.host_mode_finish_btn);
                btnBack.setOnClickListener(this);
                btnFinish.setOnClickListener(this);
                rlExistAudio.setOnClickListener(this);
                rlTextToSpeech.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
                switch(view.getId()) {
                        case R.id.host_mode_custom_imageView_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                                break;
                        case R.id.host_mode_rl_audio:
                                //默认动作
                                checkBox1.setChecked(true);
                                checkBox2.setChecked(false);
                                break;
                        case R.id.host_mode_rl_text_to_speech:
                                //指定动作
                                checkBox2.setChecked(true);
                                checkBox1.setChecked(false);
                                //跳转到指定动作界面
                                startOtherActivity(this, ActionChoiceActivity.class);
                                break;
                        case R.id.host_mode_finish_btn:
                                //右上角完成按钮
                                if(checkBox1.isChecked()) {
                                        Intent intent = new Intent();
                                        intent.putExtra("actionID","-1");
                                        intent.putExtra("actionName","默认动作");
                                        setResult(RESULT_OK,intent);
                                        finish();
                                        return;
                                }
                                if(actionID == null) {
                                        ToastTools.newInstance(this).showText(getString(R.string.please_select_action));
                                        return;
                                }
                                Intent intent = new Intent();
                                intent.putExtra("actionID",actionID);
                                intent.putExtra("actionName",actionName);
                                setResult(RESULT_OK,intent);
                                finish();
                                break;

                }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
                super.onActivityResult(requestCode, resultCode, intent);
                if(intent != null) {
                        if(requestCode == 100 && resultCode == RESULT_OK) {
                                actionID = intent.getStringExtra("actionID");
                                actionName = intent.getStringExtra("actionName");
                        }
                }
                initCheck();
        }

        @Override
        protected void onDestroy() {
                super.onDestroy();
        }

        /**
         * 跳转界面
         *
         * @param
         */
        protected void startOtherActivity(Context context, Class<?> cls) {
                Intent intent = new Intent(context, cls);
                intent.putExtra("actionID", actionID);
                intent.putExtra("actionName", actionName);
                startActivityForResult(intent, 100);
                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
        }
}
